/**
 * 
 *  @project Air Traff Monitor
 * 
 *  @author M.Emre ATASEVER <m.emre.atas@gmail.com>
 *  @author M.Burak ALKAN <mburakalkan@gmail.com>
 *  @author H.Burak KARAMIK <bkaramik@gmail.com>
 *
 *	`Air Traffic Monitor' is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "filteredit.h"


FilterEdit::FilterEdit(QWidget *parent) :  QLineEdit(parent), ag(this),acon(this),astart(this),aend(this),aeq(this),areg(this),ainv(this),acase(this),clearButton(this)
{
	QPixmap pixmap(":/img/system-search.png");
	
	acon.setText("Contains");
	acon.setCheckable(true);
	acon.setChecked(true);
	ag.addAction(&acon);
	menu.addAction(&acon);

	connect(&acon,SIGNAL(triggered()),this,SLOT(opChanged()));

	astart.setText("Starts with");
	astart.setCheckable(true);
	menu.addAction(&astart);
	connect(&astart,SIGNAL(triggered()),this,SLOT(opChanged()));
	ag.addAction(&astart);

	aend.setText("Ends with");
	aend.setCheckable(true);
	menu.addAction(&aend);
	connect(&aend,SIGNAL(triggered()),this,SLOT(opChanged()));
	ag.addAction(&aend);

	aeq.setText("Equals");
	aeq.setCheckable(true);
	menu.addAction(&aeq);
	connect(&aeq,SIGNAL(triggered()),this,SLOT(opChanged()));
	ag.addAction(&aeq);

	areg.setText("RegExp");
	areg.setCheckable(true);
	menu.addAction(&areg);
	connect(&areg,SIGNAL(triggered()),this,SLOT(opChanged()));
	ag.addAction(&areg);

	menu.addSeparator();
	acase.setText("Case sensitive");
	acase.setCheckable(true);
	menu.addAction(&acase);

	ainv.setText("Invert condition");
	ainv.setCheckable(true);
	menu.addAction(&ainv);

	inv	= cas	= false;

	clearButton.setIcon(QIcon(pixmap));
	clearButton.setIconSize(pixmap.size());
	clearButton.setCursor(Qt::ArrowCursor);
	clearButton.setStyleSheet("QToolButton { border: none; padding: 0px; }\r\nQToolButton::menu-arrow{ position: relative; top: 4px; left: 3px;}");
	clearButton.setMenu(&menu);
	clearButton.setPopupMode(QToolButton::InstantPopup);
	
	int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
	setStyleSheet(QString("QLineEdit { padding-left: %1px; } ").arg(clearButton.sizeHint().width() + frameWidth + 1));
	QSize msz = minimumSizeHint();
	setMinimumSize(qMax(msz.width(), clearButton.sizeHint().height() + frameWidth * 2 + 2),
        qMax(msz.height(), clearButton.sizeHint().height() + frameWidth * 2 + 2));

}

void FilterEdit::opChanged()
{
	if(sender() == &acon)
		emit modeChanged(Contains);
	else if(sender() == &astart)
		emit modeChanged(Starts);
	else if(sender() == &aend)
		emit modeChanged(Ends);
	else if(sender() == &areg)
		emit modeChanged(RegExp);
	else
		emit modeChanged(Equals);
}

void FilterEdit::resizeEvent(QResizeEvent *)
{
	QSize sz = clearButton.sizeHint();
	int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
	clearButton.move(frameWidth + 2,(rect().bottom() + 1 - sz.height())/2);
}
